/**
 * Torchd UI Conduit
 *
 * Manages communication between all components of torchd.ui
 *
 * @package Torchd Framework
 * @subpackage UI
 * @author Jason Raede <jason@torchdm.com>
 */
torchd.ui = {
	/** 
	 * Associative array (object?) of open dialog windows
	 */
	openDialogs:{},
	
	/**
	 * Array of menus (typically only one, but we might make varying types in the future)
	 */
	menus:[],
	
	/**
	 * The mask for modal display
	 */
	mask:false,
	
	/**
	 * Display dialogs and windows with a mask in the background?
	 */
	modalDisplay:true,
	
	/** 
	 * Maximum # of dialogs allowed to be open at once.
	 * Leave 0 for no limit
	 *
	 * Note that this is set in the init method so you
	 * don't need to touch it here. This is just default.
	 */
	maxOpenDialogs:0,
	
	/**
	 * Sets the editable parameters (modalDisplay & maxOpenDialogs)
	 */
	setParameters:function(parameters) {
		if(parameters.maxOpenDialogs) this.maxOpenDialogs = parameters.maxOpenDialogs;
		if(typeof parameters.modalDisplay != 'undefined') this.modalDisplay = parameters.modalDisplay;
	},
	
	/**
	 * Creates a dialog window with the parameters specified
	 * 
	 * Available parameters (# is required):
	 *		#{String} title				Title of the dialog window. Shows up in its header
	 *									as well as in internal pointing. 
	 *
	 *		{String} type				Type of dialog. Options are "alert", "confirm", "prompt",
	 *									or "form". Implementations of these are as you would expect.
	 *
	 *		{Object/String} message		Message to display underneath the header. Can be a string,
	 *									or an object with properties for type (used in assigning a
	 *									class to the paragraph object) and message.
	 *
	 *		{Function} onSubmit			Callback function for after the user clicks on "submit" or
	 *									"OK". Data passed to the function depends on the type of form.
	 *									For prompt or form types, the form data is passed as an array
	 *									of objects with name/value pairs for the form elements. For
	 *									confirm, this function is called with no arguments. This is not
	 *									called on alerts.
	 *
	 *		{Function} onCancel			Callback function for after the user clicks on "cancel". This
	 *									function takes no arguments.
	 *
	 *		{Array} formElements		Array of form element definitions to get passed to the 
	 *									torchd.form and torchd.form.field objects. See documentation
	 *									for those objects for valid form element definitions.
	 *								
	 *
	 * @param object params		Object with all parameters. See
	 *							online documentation for more info
	 */
	showDialog:function(params) {
		if(this.modalDisplay) {
			if(!Object.keys(this.openDialogs).length) {
				this.mask = this.createElement('div').hide().addClass('torchd-ui-mask').appendTo('body').fadeIn(100);
			}
		}
		if(!params) {
			torchd.system.error('ui.showDialog', 'You must provide parameters to open a dialog window.');
			return;
		}
	 	if(!params.title) {
	 		torchd.system.error('ui.showDialog', 'Parameters must include a title property for internal pointing.');
	 		return;
	 	}
		if(this.openDialogs[params.title]) {
			this.openDialogs[params.title].close();
			delete this.openDialogs[params.title];
		}
	 	this.openDialogs[params.title] = new torchd.ui.dialog(params);
	 },
	 
	 /**
	  * Creates a generic element using jQuery.
	  *
	  * Used in order to add the torchd-ui class to all elements.
	  *
	  * @param string elementType		The element type
	  */
	 createElement:function(elementType) {
	 	return $('<' + elementType + '/>').addClass('torchd-ui');
	 },
	 
	 /**
	  * Closes the dialog window with this specific title
	  *
	  * @param string title		The title of the window
	  */
	 closeDialog:function(title) {
	 	this.openDialogs[title].close();
	 	if(this.modalDisplay) {
			if(!Object.keys(this.openDialogs).length) {
				this.mask.remove();
			}
		}
	 },
	 
	  closeAllDialogs:function() {
	 	for(var title in this.openDialogs) {
	 		this.openDialogs[title].close();
	 	}
	 	$('.torchd-ui-mask').remove();
	 },
	 
	 /**
	  * Event handler when a dialog is closed. 
	  *
	  * Check if there are any other dialogs still open; if not,
	  * then trash the mask as well.
	  *
	  * @param string title		The title of the dialog, for internal pointing.
	  */
	 dialogClosed:function(title) {
	 	var self = this;
	 	delete this.openDialogs[title];
	 	if(this.modalDisplay && !Object.keys(this.openDialogs).length) {
	 		// Fade out the mask
	 		this.mask.fadeOut(100, function() {
	 			$(this).remove();
	 			self.mask = false;
	 		});
	 	}
	 },
	 
	 /**
	  * Display a menu bar with the given definition.
	  *
	  * See menu bar object for detailed description of menu bar definition object
	  *
	  * @param {Object} definition
	  */
	 showMenu:function(definition) {
	 	this.menus.push(new this.menuBar(definition));
	 	var self = this;
	 	$(document).click(function() {
	 		for(var i=0;i<self.menus.length;i++) {
	 			if(self.menus[i].openMenu) {
		 			self.menus[i].openMenu.hide();
					self.menus[i].openMenu.parent('li').removeClass('selected');
					delete self.menus[i].openMenu;
				}
	 		}
	 	});
	 },
	 
	 addTooltip:function(element, params) {
	 	new this.tooltip(element, params);
	 }
	 
};